package com.app.piemoment.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.piemoment.NotifyDetailActivity;
import com.app.piemoment.R;
import com.app.piemoment.entity.NotifyEntity;
import com.app.piemoment.image.ImageLoaderManager;

public class NotifyAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<NotifyEntity> list;
	private LayoutInflater listInflater;
	private ImageLoaderManager imageLoader;

	public NotifyAdapter(Context context, ArrayList<NotifyEntity> list) {
		imageLoader = new ImageLoaderManager(context, new Handler(), this);
		this.context = context;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list != null ? list.size() : 0;
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			listInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			holder = new ViewHolder();
			convertView = this.listInflater.inflate(R.layout.notify_item,null);
			
			holder.item = (RelativeLayout) convertView.findViewById(R.id.notify_layout);
			holder.imgicon = (ImageView) convertView.findViewById(R.id.imageView);
			holder.username = (TextView) convertView.findViewById(R.id.username);
			holder.desc = (TextView) convertView.findViewById(R.id.desc);
			holder.timestamp = (TextView) convertView.findViewById(R.id.timestamp);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		NotifyEntity notify = list.get(position);
		updateUI(holder,notify);
		holder.item.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				Intent intent = new Intent(context,NotifyDetailActivity.class);
//				context.startActivity(intent);
			}
		});
		return convertView;
	}

	private void updateUI(ViewHolder holder, NotifyEntity notify) {
//		holder.imgicon.setImageBitmap(imageLoader.getImage(notify.getImageUrl(), R.drawable.app_icon, 0));
		holder.username.setText(notify.getTitle());
		holder.desc.setText(notify.getDesc());
//		holder.timestamp.setText(notify.getCreateTime());
		
	}

	public final class ViewHolder {

		public ImageView imgicon;
		public TextView username;
		public TextView desc;
		public TextView timestamp;
		public RelativeLayout item;

	}

}
